package com.leiduoduo.oms.controller;

import com.leiduoduo.oms.annotation.PassToken;
import com.leiduoduo.oms.pojo.Boss;
import com.leiduoduo.oms.pojo.Token;
import com.leiduoduo.oms.service.BossService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@RestController
@Slf4j
public class BossController {
    @Autowired
    BossService bossService;

    @PostMapping("/addBoss")
    @PassToken
    public boolean addBoss(Boss boss){
        return bossService.addBoss(boss);
    }
    @PostMapping("/delBoss")
    @PassToken
    public boolean delBoss(Boss boss){
        return bossService.delBoss(boss);
    }
    @PostMapping("/upBoss")
    @PassToken
    public boolean upBoss(Boss boss){
        return bossService.upBoss(boss);
    }
    @GetMapping("/getBossByCode")
    @PassToken
    public Boss getBossById(String code){
        return bossService.getBossByCode(code);
    }
    @GetMapping("/getBossList")
    @PassToken
    public List<Boss> getBossList(){
        return bossService.getBossList();
    }
    @GetMapping("/getInByMonth")
    @PassToken
    public Map<String, Object> getInByMonth(String restaurant_id, String year){
        Map<String, Object> map = new HashMap<>();
        map.put("restaurant_id",restaurant_id);
        map.put("year",year);
        return bossService.getInByMonth(map);
    }
    @GetMapping("/getInByYear")
    @PassToken
    public Map<String, Object> getInByYear(String restaurant_id, String year){
        Map<String, Object> map = new HashMap<>();
        map.put("restaurant_id",restaurant_id);
        map.put("year",year);
        return bossService.getInByYear(map);
    }
    @GetMapping("/getDishTop10")
    @PassToken
    public List<Map<String, Object>> getDishTop10(String restaurant_id, String begin_time, String end_time ){
        List<Map<String, Object>> resultList = new LinkedList<>();
        Map<String, Object> parameters = new HashMap<>();
        parameters.put("restaurant_id",restaurant_id);
        parameters.put("begin_time",begin_time);
        parameters.put("end_time",end_time);
        for (Map<String, Object> map : bossService.getDishTop10(parameters)) {
            Map<String, Object> result = new HashMap<>();
            result.put("value",map.get("dish_number"));
            result.put("name",map.get("name"));
            resultList.add(result);
        }
        return resultList;
    }
    @PostMapping("/bossLogin")
    @PassToken
    public Map<String, Object> bossLogin(Boss boss){
        Map<String, Object> result = new HashMap<>();
        Token token = new Token();
        String stoken= null;
        boss = bossService.getBossByCode(boss.getCode());
        if (boss.getPassword().equals(boss.getPassword())) {
            stoken = token.getToken(boss.getCode());
            boss.setToken(stoken);
            if (bossService.uptoken(boss)) {
                result.put("msg","success");
                result.put("errorcode",0);
                result.put("boss",boss);
            } else {
                result.put("msg","token修改失败");
                result.put("errorcode",500);
                result.put("boss","null");
            }
        } else {
            result.put("msg","用户名或密码错误");
            result.put("errorcode",401);
            result.put("boss","null");
        }
        return result;
    }
}
